<?php
declare (strict_types = 1);

namespace app\controller\user\v1;

use app\BaseController;
use think\facade\Db;

class Order extends BaseController
{
    /**
     * 获取订单列表
     * @return \think\Response
     */
    public function getList()
    {
        $data = $this->request->data;
        $page = $data['page'] ?? 1;
        $limit = $data['limit'] ?? 10;
        $wxappUserUuid = $this->request->uuid;
        
        $phone = Db::table('wxappUser')->where('uuid',$wxappUserUuid)->value('cellPhone');
        if(!$phone) return result(SUCCESS, '', []);
        
        $where= [
            ['phone','=',$phone],
            ['isDelete','=',0],
        ];
        if(isset($data['status'])&&$data['status']!=''){
            $where[] = ['status','=',$data['status']];
        }   
        if(isset($data['keyword'])){
            $where[] = ['name', 'like', "%{$data['keyword']}%"];
        }
        
        $list = Db::table('project')
            ->where($where)
            ->field('id,orderNo,name,createTime,status as statusText')
            ->order('id', 'desc')
            ->page((int)$page, (int)$limit)
            ->select()
            ->toArray();
        
        $status = [
            '审核中'=>'pending',
            '已交付'=>'paid',
            '未通过'=>'rejected',
            '优化中'=>'optimizing',
            '未成功'=>'failed',
        ];
        //添加一个firstName 提取nme里面的第一个字符
        foreach ($list as $key => $value) {
            $list[$key]['firstName'] = mb_substr($value['name'], 0, 1);
            $list[$key]['status'] = $status[$value['statusText']];
        }

        $total = Db::table('project')->where($where)->count();

        return result(SUCCESS, '获取成功', [
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'limit' => $limit
        ]);
    }

    /**
     * 获取订单详情
     * @return \think\Response
     */
    public function getDetail()
    {
        $data = $this->request->data;
        $id = $data['id'];
        $wxappUserUuid = $this->request->uuid;
        $phone = Db::table('wxappUser')->where('uuid',$wxappUserUuid)->value('cellPhone');
        if(!$phone) return result(FAIL, '用户不存在');
        $project = Db::table('project')->where('id',$id)->where('phone',$phone)->field('name,status')->find();
        if(!$project) return result(FAIL, '订单不存在');

       $data = Db::table('projectKeyword')
       ->where('projectId',$id)
       ->where('isDelete',0)
       ->field('id,concat(region,business,question) as name,status')
       ->select()
       ->toArray();

       $info = [
        'name'=>$project['name'],
        'status'=>$project['status'],
        'list'=>$data,
       ];

        return result(SUCCESS, '获取成功', $info);
    }

    /**
     * 获取订单详情的图片
     * @return \think\Response
     */
    public function getDetailImage()
    {
        $data = $this->request->data;
        $date = $data['date'];
        $keywordId = $data['id'];
        $projectId = $data['projectId'];
        $wxappUserUuid = $this->request->uuid;

        $phone = Db::table('wxappUser')->where('uuid',$wxappUserUuid)->value('cellPhone');
        if(!$phone) return result(SUCCESS, '', []);

        $project = Db::table('project')->where('id',$projectId)->where('phone',$phone)->field('name,status')->find();
        if(!$project) return result(FAIL, '订单不存在');

        $keyword = Db::table('projectKeyword')->where('id',$keywordId)->value('concat(region,business,question) as name');
        if(!$keyword) return result(FAIL, '关键词不存在');
    
        //#date格式是2025-03 表的date是2025-03-27  需要找到2025-03 整个月的所有数据
        $startDate = $date.'-01';
        $endDate = date('Y-m-t', strtotime($date));
        $images = Db::table('keywordImage')->where('keywordId',$keywordId)->where('isDelete',0)->whereBetween('date',[$startDate,$endDate])->column('imageUrl');
        $info = [
            'name'=>$keyword,
            'list'=>$images,
        ];

        return result(SUCCESS, '获取成功', $info);
    }

    /**
     * 获取订单选项
     * @return \think\Response
     */
    public function getOptions(){
        $data = $this->request->data;
        $projectId = $data['projectId'];
        $wxappUserUuid = $this->request->uuid;
        $phone = Db::table('wxappUser')->where('uuid',$wxappUserUuid)->value('cellPhone');
        if(!$phone) return result(FAIL, '用户不存在');
        $project = Db::table('project')->where('id',$projectId)->field('id,name')->select()->toArray();
        if(!$project) return result(FAIL, '订单不存在');

        $data = Db::table('projectKeyword')
        ->where('projectId',$projectId)
        ->where('isDelete',0)
        ->field('id as value, concat(region,business,question) as name')
        ->select()
        ->toArray();
        
        return result(SUCCESS, '', $data);
    }
} 